package com.example.blog.controller;


import com.example.blog.Constants.Constants;
import com.example.blog.model.Result;
import com.example.blog.model.UserInfo;
import com.example.blog.service.UserService;
import com.example.blog.utils.JwtUtils;
import com.example.blog.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Result login(String userName, String passWord){
        log.info("UserController#login接收参数：username：{}，password:{}",userName,passWord);
        //1.校验参数合法性
        //2.校验密码是否正确
        //3.密码正确返回token
        //4.密码错误返回错误信息
        if (!StringUtils.hasLength(userName)||!StringUtils.hasLength(passWord)){
            return Result.fail("账号或密码不能为空");
        }
        //从数据库中查找用户
        UserInfo userInfo = userService.selectByName(userName);
        //用户不存在
        if (userInfo==null){
            log.error("用户不存在");
            return Result.fail("用户不存在");

        }
        //密码错误
        if (!SecurityUtils.verify(passWord,userInfo.getPassWord())){
            log.error("密码错误");
            return Result.fail("密码错误");
        }
        //密码正确
        Map<String,Object> claim = new HashMap<>();
        claim.put(Constants.TOKEN_ID,userInfo.getId());
        claim.put(Constants.TOKEN_USERNAME,userInfo.getUserName());
        String token = JwtUtils.genJwtToken(claim);
        log.info("UserController#login 返回结果token:{}",token);
        return Result.success(token);

    }
    //在博客列表页，获取当前登录用户的信息(从token中拿)
    @RequestMapping("/getUserInfo")
    public UserInfo getLoginUserInfo(HttpServletRequest request){
        log.info("获取用户信息.....");
        //获取token
       String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
       //从token中获取登录用户的ID
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId==null){
            //用户未登录
            return null;
        }
        UserInfo userInfo = userService.selectById(userId);
        return userInfo;
    }

    //在博客详情页，获取当前文章作者信息
    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId){

        if (blogId<=0){
            return null;
        }
        UserInfo userInfo = userService.getAuthorInfo(blogId);
        return userInfo;
    }
}
